<?php
/*
Author: Nicolas Zdanovicz
Date 20140325
User Data Access Class
*/
class DAOClase{

    public static function getById($id){
        
         $db = getPDO();
         $sql = "select id, id_horario_clase as horario, fecha, vacantes from clase where id = ?";
         $stmt = $db->prepare($sql);
         $ok = $stmt->execute(array($id));
         $obj = $stmt->fetchObject();
         
         $cla = null;
         if($obj!=null){
             $cla= DAOClase::mapValues($obj);
         }
         else{
            throw new ENoDataFound("No se encontro la clase con el id ".$id);
         }
         
         return $cla;
    }
    
    



    public static function getAll(){
         
         $db = getPDO();
         $sql = "select id, id_horario_clase as horario, fecha, vacantes from clase";
         $stmt = $db->prepare($sql);
         $ok = $stmt->execute();
         $result = $stmt->fetchAll(\PDO::FETCH_OBJ);
         $arrResult = array();    
        
         foreach($result as $row){             
             array_push($arrResult,DAOClase::mapValues($row));
         }
         
        return $arrResult;
    }
    
    
    
    
    
    public static function add($clase){
        
         $db = getPDO();
          $sql = "insert into clase (id_horario_clase, fecha, vacantes) values ";
          $sql.= "(?,?,?)";
          $stmt = $db->prepare($sql);
          $ok = $stmt->execute(array($clase->getHorario(), $clase->getFecha(), $clase->getVacantes() ));
        $clase->setId($db->lastInsertId());
        
         return $clase;        
    }
     
    public static function update($clase){
    
    	$db = getPDO();
    	$sql = "update clase set vacantes = ? where id =?";
    	$stmt = $db->prepare($sql);
    	$ok = $stmt->execute(array(
    			$clase->getVacantes(),
    			$clase->getId()));
    	return;
    }
    
    private static function mapValues($obj){
        $cla = new Clase();
		
		$cla->setId($obj->id);
		$cla->setHorario($obj->horario);
		$cla->setFecha($obj->fecha);
		$cla->setVacantes($obj->vacantes);
        
        return $cla;
    }
    
}
?>